package com.ludomancy.breathe;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.Vector;

import com.ludomancy.gamelib.Button;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Widget;
import com.ludomancy.gamelib.Button.ButtonListener;

public class OptionsWidget extends Widget implements ButtonListener
{
	private Vector buttons = new Vector();
	private static int BUTTON_WIDTH = 200;
	private static int BUTTON_HEIGHT = 30;
	private static int BUTTON_GAP = 5;
	
	public OptionsWidget(Widget parent, Rectangle area)
	{
		super(parent, area);
		changeOptions(Game.game.options);
	}
	
	public void changeOptions(Vector options)
	{
		int offsetX = (getWidth()-BUTTON_WIDTH)/2;
		int offsetY = BUTTON_GAP;
		// remove old options
		for (Iterator iter = buttons.iterator(); iter.hasNext();)
		{
			Button button = (Button) iter.next();
			button.close();
		}
		
		buttons = new Vector();
		
		if(options.contains(Game.restart))
		{
			offsetY += BUTTON_HEIGHT*5;
		}
		
		int baseNdx = 0;
		if(options.contains(Game.struggle))
		{
			options.remove(Game.struggle);
			options.insertElementAt(Game.struggle, 0);
			baseNdx++;
		}
		if(options.contains(Game.breathe))
		{
			options.remove(Game.breathe);
			options.insertElementAt(Game.breathe, baseNdx);
		}
		
		// create an option button per choice
		for (Iterator iter = options.iterator(); iter.hasNext();)
		{
			Option option = (Option) iter.next();
			Rectangle buttonArea = new Rectangle();
			buttonArea.x = offsetX;
			buttonArea.y = offsetY;
			buttonArea.width = BUTTON_WIDTH;
			buttonArea.height = BUTTON_HEIGHT;
			buttons.add(new OptionButton(this, buttonArea, option, this ));
			offsetY += BUTTON_HEIGHT + BUTTON_GAP;
		}
	}

	public void paint(Graphics g)
	{
		if(Game.game.state == Game.STATE_WIN)
		{
			g.drawImage(ResourceManager.getImage("cat.png"), 0, 0, null);
		}
	}

	public void onPress(Button widget)
	{
		Option chosen = ((OptionButton)widget).option;
		Game.game.chooseOption(chosen);
	}
}
